﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using LTP.Accounts.Bus;
using Things.Common;

namespace Things.ThingsWeb.Components
{
    public partial class Login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            if ((Session[ConstantString.SessionPassErrorCountAdmin] != null) && (Session[ConstantString.SessionPassErrorCountAdmin].ToString() != ""))
            {
                int PassErroeCount = Convert.ToInt32(Session[ConstantString.SessionPassErrorCountAdmin]);
                if (PassErroeCount > 3)
                {
                    txtUsername.Disabled = true;
                    txtPass.Disabled = true;
                    btnLogin.Enabled = false;
                    this.lblMsg.Text = "对不起，你错误登录了三次，系统登录锁定！";
                    return;
                }

            }

            if ((Session[ConstantString.SessionCheckCode] != null) && (Session[ConstantString.SessionCheckCode].ToString() != ""))
            {
                if (Session[ConstantString.SessionCheckCode].ToString().ToLower() != this.CheckCode.Value.ToLower())
                {
                    this.lblMsg.Text = "所填写的验证码与所给的不符 !";
                    Session[ConstantString.SessionCheckCode] = null;
                    return;
                }
                else
                {
                    Session[ConstantString.SessionCheckCode] = null;
                }
            }
            else
            {
                Response.Redirect("~/login.aspx");
            }

            string userName = Things.Common.PageValidate.InputText(txtUsername.Value.Trim(), 30);
            string Password = DESEncryptDecrypt.Encrypt(Things.Common.PageValidate.InputText(txtPass.Value.Trim(), 30));

            Things.BLL.Buyer bll = new BLL.Buyer();
            Things.Model.Buyer newUser = bll.GetModel(userName, Password); // to do


            if (newUser == null)
            {
                this.lblMsg.Text = "登陆失败： " + userName;
                if ((Session[ConstantString.SessionPassErrorCountAdmin] != null) && (Session[ConstantString.SessionPassErrorCountAdmin].ToString() != ""))
                {
                    int PassErroeCount = Convert.ToInt32(Session[ConstantString.SessionPassErrorCountAdmin]);
                    Session[ConstantString.SessionPassErrorCountAdmin] = PassErroeCount + 1;
                }
                else
                {
                    Session[ConstantString.SessionPassErrorCountAdmin] = 1;
                }
            }
            else
            {

                Session[ConstantString.SessionCurrentBuyerAccount] = newUser;
              
                if (Session[ConstantString.SessionSourceURL] != null)
                {
                    string returnpage = Session[ConstantString.SessionSourceURL].ToString();
                    Session[ConstantString.SessionSourceURL] = null;
                    Response.Redirect(returnpage);
                }
                else
                {
                    
                    Response.Redirect(ConstantString.HomePageURL);
                }
            }
        }


    }
}